package com.dhsc.service.impl.ywjk.qywjh.jhlxwh;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dhsc.domain.jkzx.ywjk.qywjh.jhlxwh.TjkYclbVo;
import com.dhsc.domain.jkzx.ywjk.qywjh.jhlxwh.TjkYclbYclxVo;
import com.dhsc.domain.jkzx.ywjk.qywjh.jhmxxx.TjkYclxVo;
import com.dhsc.domain.yypt.LayuiTableVo;
import com.dhsc.domain.yypt.ResponseObject;
import com.dhsc.exception.MyException;
import com.dhsc.local.dao.ywjk.qywjh.jhlxwh.ILxflcsDao;
import com.dhsc.service.ywjk.qywjh.jhlxwh.ILxflcsService;
import com.dhsc.util.CommonUtil;
import com.github.pagehelper.PageInfo;

import lombok.extern.slf4j.Slf4j;

/**
 * @ClassName: LxflcsServiceImpl
 * @Description: 类型分类参数
 * @Author: jwj
 * @Date: 2023/11/7 11:06
 */
@Service
@Slf4j
public class LxflcsServiceImpl implements ILxflcsService {

    @Autowired
    private ILxflcsDao lxflcsDao;


    @Override
    public LayuiTableVo queryLxflcs() {
        LayuiTableVo obj = new LayuiTableVo();
        try{
            List<TjkYclbVo> list = lxflcsDao.queryLxflcs();
            PageInfo<TjkYclbVo> pageInfo = new PageInfo<>(list);
            obj.setData(list);
            obj.setMsg("");
            obj.setCount(pageInfo.getTotal());
            obj.setCode(0);
        }catch(Exception e){
            log.error("查询类型分类参数信息出错", e);
            throw new MyException("查询类型分类参数信息出错",e);
        }
        return obj;
    }

    @Override
    public ResponseObject queryLxflcsYclx() {
        ResponseObject obj = new ResponseObject();
        try{
            List<TjkYclxVo> list = lxflcsDao.queryLxflcsYclx();
            obj.setData(list);
            obj.setStatus(200);
        }catch(Exception e){
            log.error("查询类型分类参数异常类型出错", e);
            throw new MyException("查询类型分类参数异常类型出错",e);
        }
        return obj;
    }

    @Transactional("localTransactionManager")
    @Override
    public ResponseObject saveLxflcs(TjkYclbVo tjkYclbVo) {
        ResponseObject obj = new ResponseObject();
        try{
            //查看是否存在代码或名称相同的记录
            int count=lxflcsDao.queryYclbExist(tjkYclbVo);
            if(count>0){
                obj.setStatus(500);
                obj.setMessage("类别代码或名称已存在，无法更新！");
                return obj;
            }
            //编号存在则是更新，否则新增
            if(CommonUtil.isNotEmptyObj(tjkYclbVo.getBh())){
                lxflcsDao.updateYclb(tjkYclbVo);
                lxflcsDao.delYclbYclx(tjkYclbVo.getBh());
            }else{
                int bh=lxflcsDao.querySjkYclbBh();
                tjkYclbVo.setBh(bh);
                lxflcsDao.saveYclb(tjkYclbVo);
            }
            String yclxCidStr=tjkYclbVo.getYclxCidStr();
            if(StringUtils.isNotBlank(yclxCidStr)){
                List<String> cidStrList= Arrays.asList(yclxCidStr.split(","));
                List<TjkYclbYclxVo> tjkYclbYclxVos=new ArrayList<>();
                for(String cid:cidStrList){
                    TjkYclbYclxVo tjkYclbYclxVo=new TjkYclbYclxVo();
                    tjkYclbYclxVo.setBh(tjkYclbVo.getBh());
                    tjkYclbYclxVo.setCid(Integer.parseInt(cid));
                    tjkYclbYclxVos.add(tjkYclbYclxVo);
                }
                if(CommonUtil.isNotEmptyList(tjkYclbYclxVos)){
                    lxflcsDao.saveYclbYclx(tjkYclbYclxVos);
                }
            }
            obj.setStatus(200);
        }catch(Exception e){
            log.error("保存类型分类参数出错", e);
            throw new MyException("保存类型分类参数出错",e);
        }
        return obj;
    }

    @Transactional("localTransactionManager")
    @Override
    public ResponseObject deleteLxflcs(TjkYclbVo tjkYclbVo) {
        ResponseObject obj = new ResponseObject();
        try{
            lxflcsDao.deleteYclb(tjkYclbVo.getBh());
            lxflcsDao.delYclbYclx(tjkYclbVo.getBh());
            obj.setStatus(200);
        }catch(Exception e){
            log.error("删除类型分类参数出错", e);
            throw new MyException("删除类型分类参数出错",e);
        }
        return obj;
    }
}
